<?php
class Account_log_model extends CI_Model
{
    private $table = 'account_log';
    
    public function insertAccountLogUser($order_id, $uid, $amount, $amount_org, $cat_id, $account_type=1)
    {
        $data = array(
            'order_id'     => $order_id,
            'uid'          => $uid,
            'date'         => date('Y-m-d H:i:s',time()),
            'amount'       => $amount,
            'amount_carry' => $amount_org,
            'type'         => $cat_id, //16代表后台虚拟充值
            'account_type' => $account_type, //不知道啥意思
            'product_type' => 'tourism',
            'remittance_person' => $uid
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function insertAccountLogProfit($orderProfitItem, $amount_carry, $type)
    {
        $data = array(
            'order_id'     => $orderProfitItem->order_id,
            'uid'          => $orderProfitItem->uid,
            'amount'       => $orderProfitItem->account,
            'amount_carry' => $amount_carry,
            'date'         => date('Y-m-d H:i:s',time()),
            'type'         => $type, //16代表后台虚拟充值
            'account_type' => $orderProfitItem->account_type, 
            'product_type' => 'line',
        );
        $this->db->insert($this->table,$data);
        return $this->db->insert_id();
    }

    /**
     * 划款记录
     * @param $order_id
     * @param $uid
     * @param $amount
     * @param $amount_org
     * @param $type
     * @param int $account_type
     * @return mixed
     */
    public function insertAccountLogAdvance($order_id, $uid, $amount, $amount_org, $type, $account_type=1)
    {
        $data = array(
            'order_id'     => $order_id,
            'uid'          => $uid,
            'amount'       => $amount,
            'amount_carry' => $amount_org,
            'date'         => date('Y-m-d H:i:s',time()),
            'type'         => $type, //16代表后台虚拟充值
            'account_type' => $account_type,
            'product_type' => 'tourism',
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function findAccountLogParam($paramArray=array())
    {
        $this->db->where('order_id', $paramArray['order_id']);
        $this->db->where('product_type', $paramArray['product_type']);
        return $this->db->get($this->table);
    }

    public function insertBatchAccountLog($postData)
    {
        $data = [];
        $i = 0;
        foreach ($postData as $value) {
            $data[$i] = [
                'order_id'     => $value->order_id,
                'uid'          => $value->uid,
                'amount'       => $value->account,
                'date'         => date('Y-m-d H:i:s'),
                'type'         => $value->type, //16代表后台虚拟充值
                'account_type' => $value->account_type,
                'product_type' => 'tourism',
            ];
            $i++;
        }
        return $this->db->insert_batch($this->table, $data);
    }

    public function insertBatch($postData = [])
    {
        if (empty($postData)) {
            return false;
        }
        return $this->db->insert_batch($this->table, $postData);
    }

    /**
     * 运费记录
     * @param $data
     * @return mixed
     */
    public function insertAccountLogFreight($data)
    {
        $data = array(
            'order_id'     => $data['order_id'],
            'uid'          => $data['uid'],
            'amount'       => $data['amount'],
            'amount_carry' => $data['amount_carry'],
            'date'         => date('Y-m-d H:i:s',time()),
            'type'         => $data['type'], //16代表后台虚拟充值
            'account_type' => 1,
            'product_type' => 'tourism',
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    /**
     * 分页
     */
    public function amountLogTotal($params)
    {
        $this->db->select('log_id');
        $this->db->from($this->table);
        $this->db->where('uid', $params['uid']);
        if ( isset($params['type']) &&  $params['type'] == 'disburse' ) //支出
        {
            $this->db->where_in('type', array(PAY, CASH, DEDUCTION)); 
        }
        elseif (isset($params['type']) &&  $params['type'] == 'income' )
        {
             $this->db->where_not_in('type', array(PAY, CASH, DEDUCTION)); 
        }
        elseif (! empty($params['type'])) 
        {
            $this->db->where_in('type', $params['type']);
        }
        return $this->db->count_all_results();
    }

    /**
     * 查找用户充值明细
     */
    public function amountLogList($params, $num, $start)
    {
        $this->db->select('amount, date, type, account_type, product_type');
        $this->db->from($this->table);
        $this->db->where('uid', $params['uid']);

        if (isset($params['type']) &&  $params['type'] == 'disburse' ) //支出
        {
            $this->db->where_in('type', array(PAY, CASH, DEDUCTION)); 
        }
        elseif (isset($params['type']) &&  $params['type'] == 'income' )
        {
             $this->db->where_not_in('type', array(PAY, CASH, DEDUCTION)); 
        }
        elseif ( ! empty($params['type'])) 
        {
            $this->db->where_in('type', $params['type']);
        }
        $this->db->order_by('log_id', 'DESC');
        $this->db->limit($start, $num);
        return $this->db->get();
    }

    /**
     * 充值记录
     * @param $depositInfo
     * @param $amountCarry
     * @return mixed
     */
    public function insertAccountLogDeposit($depositInfo, $amountCarry)
    {
        $data = array(
            'order_id'     => $depositInfo->deposit_id,
            'uid'          => $depositInfo->uid,
            'amount'       => $depositInfo->amount,
            'amount_carry' => $amountCarry,
            'date'         => date('Y-m-d H:i:s'),
            'type'         => 16, //16代表后台虚拟充值
            'account_type' => 1,
            'product_type' => 'deposit',  //deposit 充值
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    public function insertAccountLog($params = [])
    {
        $data = array(
            'order_id'     => isset($params['order_id']) ? $params['order_id'] : 0,
            'uid'          => $params['uid'],
            'amount'       => $params['amount'],
            'date'         => date('Y-m-d H:i:s'),
            'type'         => $params['type'],
            'account_type' => isset($params['account_type']) ? $params['account_type'] : ACCOUNT_TYPE_CARRY,
            'product_type' => isset($params['product_type']) ? $params['product_type'] : 'scenic',
        );
        if (!empty($data['amount_carry'])) {
            $data['amount_carry'] = $params['amount_carry'];
        }
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
}